import type { RouteRecordNormalized } from 'vue-router';
import { UserState } from '@/store/modules/user/types';
import type { Protocol } from './types';
import { request } from './http';

/**
 * 登录用户
 */
export interface LoginUser {
  id?: number; // 自增主键id
  userCode?: string; // 用户编号
  email?: string; // 用户编号
  abbreviation?: string; // 用户简称
  fullName?: string; // 用户全称
  tel?: string; // 电话
  address?: string; // 地址
  avatar?: string; // 头像
  sessionId?: string;
  expireAt?: string;
  role?: string;
}

export interface LoginData {
  loginType: string;
  username: string;
  password: string;
}

export interface LoginRes {
  token: string;
}

export function sendCaptcha(email: string): Promise<Protocol<number>> {
  return request.get<Protocol<number>>(`/api/user/captcha`, { email });
}

export function login(data: LoginData): Promise<Protocol<LoginUser>> {
  return request.json<Protocol<LoginUser>>(`/api/user/login`, data);
}

export function currentUser(): Promise<Protocol<LoginUser>> {
  return request.get<Protocol<LoginUser>>(`/api/user/current`, {});
}

export function logout() {
  return request.post<LoginRes>('/api/user/logout');
}

export function getUserInfo() {
  return request.post<UserState>('/api/user/info');
}

export function getMenuList() {
  return request.post<RouteRecordNormalized[]>('/api/user/menu');
}
